﻿using Ledict.ConditionExtract.Model;
using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.DAL
{
    /// <summary>
    /// 获取
    /// </summary>
    public class ReturnModeDAL
    {
        private string DbName;
        public ReturnModeDAL()
        {
            DbName = "LdtCommonManage";
        }
        /// <summary>
        /// 增加ReturnMode表数据
        /// </summary>
        public bool ReturnModeAdd(ReturnMode model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"insert into ReturnMode(
			RM_ID, RM_Code, RM_CodeZH, RM_IsValue, RM_JoinMode, RM_UpdateTime)
			values (@RM_ID,@RM_Code,@RM_CodeZH,@RM_IsValue,@RM_JoinMode,@RM_UpdateTime)");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@RM_ID", DbType.String, model.RM_ID);
            oBase.AddInParameter(oCmd, "@RM_Code", DbType.String, model.RM_Code);
            oBase.AddInParameter(oCmd, "@RM_CodeZH", DbType.String, model.RM_CodeZH);
            oBase.AddInParameter(oCmd, "@RM_IsValue", DbType.Int32, model.RM_IsValue);
            oBase.AddInParameter(oCmd, "@RM_JoinMode", DbType.String, model.RM_JoinMode);
            oBase.AddInParameter(oCmd, "@RM_UpdateTime", DbType.DateTime, model.RM_UpdateTime);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除ReturnMode表数据
        /// </summary>
        public bool ReturnModeDelete(string rmId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ReturnMode ");
            strSql.Append(" where RM_ID=@RM_ID");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@RM_ID", DbType.String, rmId);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 修改ReturnMode表数据
        /// </summary>
        public bool ReturnModeUpdate(ReturnMode model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"  update [dbo].[ReturnMode]
                             set RM_Code = @RM_Code,
                             RM_CodeZH = @RM_CodeZH,
                             RM_IsValue = @RM_IsValue,
                             RM_JoinMode = @RM_JoinMode,
                             RM_UpdateTime = @RM_UpdateTime
                             where RM_ID = @RM_ID");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@RM_ID", DbType.String, model.RM_ID);
            oBase.AddInParameter(oCmd, "@RM_Code", DbType.String, model.RM_Code);
            oBase.AddInParameter(oCmd, "@RM_CodeZH", DbType.String, model.RM_CodeZH);
            oBase.AddInParameter(oCmd, "@RM_IsValue", DbType.Int32, model.RM_IsValue);
            oBase.AddInParameter(oCmd, "@RM_JoinMode", DbType.String, model.RM_JoinMode);
            oBase.AddInParameter(oCmd, "@RM_UpdateTime", DbType.DateTime, model.RM_UpdateTime);


            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取ReturnMode表数据
        /// </summary>
        public DataTable GetReturnModeData(string rmId)
        {
            string strSql = string.Format(@" select * from ReturnMode where RM_ID ='{0}'  ",rmId);        
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable dt = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (dt!=null&&dt.Rows.Count>0)
            {
                return dt;
            }
            return new DataTable();
        }
    }
}
